<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input v-model="query.name" clearable size="small" placeholder="请输入楼栋名称" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery">
          </el-input>
          <el-input v-model="query.unitNum" clearable size="small" placeholder="请输入楼栋编号" style="margin-left:20px; width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery">

          </el-input>
          <rrOperation :crud="crud" />
        </div>
        <crudOperation :permission="permission" />
      </div>
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="楼栋名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="楼栋编号" prop="floorNum">
            <el-input v-model="form.floorNum" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="建筑面积" prop="floorArea">
            <el-input v-model="form.floorArea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" default-expand-all :data="crud.data" row-key="id"
                @select="crud.selectChange" @select-all="crud.selectAllChange" @selection-change="crud.selectionChangeHandler">
        <el-table-column :selectable="checkboxT" type="selection" width="55" />
        <el-table-column v-permission="['admin','dept:edit','dept:del']" label="操作" width="130px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-dle="scope.row.id === 1"
              msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('name')" label="用户名称" prop="nickname" />
        <el-table-column v-if="columns.visible('name')" label="商品名称" prop="goodsName" />
        <el-table-column v-if="columns.visible('name')" label="收藏时间" prop="addTime" />
      </el-table>
    </el-card>
  </div>
</template>

<script>
  import crudCollect from '@/api/ceamGoodsCollect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import CRUD, { presenter, header, form, crud } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'

  // crud交由presenter持有
  const defaultCrud = CRUD({ title: '会员收藏', url: 'api/shop/collect', crudMethod: { ...crudCollect }})
  const defaultForm = { id: null, name: null, pid: 1, enabled: 'true' }
  export default {
    name: 'Collect',
    components: { crudOperation, rrOperation, udOperation },
    mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
    data() {
      return {
        rules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ]
        },
        permission: {
          add: ['admin', 'dept:add'],
          edit: ['admin', 'dept:edit'],
          del: ['admin', 'dept:del']
        },
        enabledTypeOptions: [
          { key: 'true', display_name: '正常' },
          { key: 'false', display_name: '禁用' }
        ]
      }
    },
    methods: {

      // 新增与编辑前做的操作
      [CRUD.HOOK.afterToCU](crud, form) {
        console.log("新增")
      },
      // 提交前的验证
      [CRUD.HOOK.afterValidateCU]() {
        if (this.form.pid !== null && this.form.pid === this.form.id) {
          this.$message({
            message: '上级部门不能为空',
            type: 'warning'
          })
          return false
        }
        if (this.form.isTop === '1') {
          this.form.pid = null
        }
        return true
      },
      // 改变状态
      changeEnabled(data, val) {
        this.$confirm('此操作将 "' + this.dict.label.dept_status[val] + '" ' + data.name + '部门, 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          crudDept.edit(data).then(res => {
            this.crud.notify(this.dict.label.dept_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
          }).catch(err => {
            data.enabled = !data.enabled
            console.log(err.response.data.message)
          })
        }).catch(() => {
          data.enabled = !data.enabled
        })
      },
      checkboxT(row, rowIndex) {
        return row.id !== 1
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
